import {BaseApi} from "./BaseApi.ts";
import type {FavorForum, ForumSearchResult} from "../types/Forum.ts";

const favorParams = (action: "add" | "del" | "get") => {
    return {__lib: "forum_favor2", __act: "forum_favor", action}
}

export class ForumApi extends BaseApi {
    // 查询收藏版面
    favorList = () => this.getNuke(favorParams("get")).then(res => res as FavorForum[])
    // 添加收藏版面
    favorAdd = (fid?: number | undefined, stid?: number | undefined) => this.postNuke({...favorParams("add"), fid, stid})
    // 删除收藏版面
    favorDel = (fid?: number | undefined, stid?: number | undefined) => this.postNuke({...favorParams("del"), fid, stid})
    // 搜索版面
    search = (key: string, page?: number | undefined) => this.instance.get("forum.php", {params: {key, page, __output: 11}})
        .then(res => {
            console.log(res.data.data)
            return res.data.data as ForumSearchResult[];
        })

    // 修改对子版面的屏蔽状态
    blockSubForum = (forumId: number, mirrorId: number, type: 'add' | 'del') => {
        const params = {
            fid: forumId,
            __lib: 'user_option',
            __act: 'set',
            type: '1',
            info: 'add_to_block_tids',
        }
        const p1 = type === 'del' ? {del: mirrorId} : {add: mirrorId}
        return this.postNuke({...params, ...p1})
    }


    getNuke = (params: any) => this.instance.get("nuke.php", {params: {...params, __output: 11,}})
        .then(res => res.data.data)
    postNuke = (params: any) => this.instance.post("nuke.php", {...params, __output: 11,})
        .then(res => res.data.data)

}

